<template>
  <div class="employees" v-loading="loading">
    <el-card class="btn">
      <!-- <el-button
        type="primary"
        @click="goImport"
        v-if="
          $store.state.user.userInfo.roles.points.includes(
            'employees_import_button'
          )
        "
       
        >导入</el-button
      > -->
      <el-button
        type="primary"
        @click="goImport"
        v-control="'employees_import_button'"
        >导入</el-button
      >
      <!-- 自定义指令控制 -->
      <el-button type="primary" @click="add" v-control="'employees_add_button'"
        >+ 新增员工</el-button
      >
    </el-card>
    <el-card>
      <el-table :data="list">
        <el-table-column label="序号">
          <template v-slot="{ $index }">
            <div>
              {{ (page.page - 1) * page.size + $index + 1 }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="姓名" prop="username"></el-table-column>
        <el-table-column label="头像" prop="staffPhoto">
          <template v-slot="{ row }">
            <img
              @click="imgClick(row.staffPhoto)"
              class="avatar"
              :src="row.staffPhoto"
              alt=""
              v-imgerror="errorUrl"
            />
          </template>
        </el-table-column>
        <!-- 手机号 -->
        <el-table-column
          label="手机号"
          prop="mobile"
          width="120px"
        ></el-table-column>
        <el-table-column label="工号" prop="workNumber"></el-table-column>
        <!-- :formatter="formatter" -->
        <el-table-column label="聘用形势" prop="formOfEmployment">
          <!-- <template v-slot="{ row }">
            <div v-hireType="row.formOfEmployment">
              {{ row.formOfEmployment | formatType }}
            </div>
          </template> -->
        </el-table-column>
        <el-table-column label="部门" prop="departmentName"></el-table-column>
        <el-table-column label="入职时间" prop="timeOfEntry">
          <template v-slot="{ row }">
            <div>
              {{ row.timeOfEntry | formatTime }}
            </div>
          </template>
        </el-table-column>
        <!-- <el-table-column label="是否在职"></el-table-column> -->
        <el-table-column label="状态" prop="enableState"></el-table-column>
        <el-table-column label="操作" width="300" fixed="right">
          <template v-slot="{ row }">
            <div>
              <el-button type="text" @click="goDetail(row.id)">查看</el-button>
              <el-button type="text">转正</el-button>
              <el-button type="text">调岗</el-button>
              <el-button type="text">离职</el-button>
              <el-button type="text" @click="roleClick(row.id)">角色</el-button>
              <el-button type="text" @click="del(row.id)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="page">
        <!--     @size-change="handleSizeChange"
          
           -->
        <el-pagination
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
          :current-page="page.page"
          :page-size="page.size"
          :total="page.total"
          :page-sizes="[1, 5, 10, 20, 200, 300, 400]"
          layout="total, sizes, prev, pager, next, jumper"
        >
        </el-pagination>
      </div>
    </el-card>
    <!-- 新增 弹框 -->
    <!-- <Dialog :isShow="show" @update:isShow="show = $event"></Dialog> -->
    <Dialog :isShow.sync="show" @getData="getData"></Dialog>
    <!-- 头像二维码 -->
    <el-dialog title="二维码" :visible.sync="showQrcode">
      <!-- dialog内的内容是在第一次打开弹框后才渲染的 -->
      <div style="text-align:center">
        <canvas ref="canvas"></canvas>
      </div>
    </el-dialog>
    <!-- 分配角色组件 -->
    <DialogRole ref="dialogRole"></DialogRole>
  </div>
</template>
<script>
import { sysUser, sysUserDel } from '@/api/employees.js'
import employees from '@/assets/constant/employees.js'
import Dialog from './components/dialog.vue'
import QrCode from 'qrcode'
import DialogRole from './components/dialogRole.vue'
export default {
  components: {
    Dialog,
    DialogRole
  },
  data () {
    return {
      showQrcode: false,
      loading: false,
      errorUrl: require('@/assets/common/head.jpg'),
      show: false,
      list: [],
      page: {
        total: 100,
        page: 1,
        size: 10
      }
    }
  },
  created () {
    this.getData()
  },
  methods: {
    async getData () {
      this.loading = true
      const res = await sysUser(this.page)
      this.loading = false
      res.data.rows.forEach(item => {
        item.formOfEmployment = this.formatter(
          null,
          null,
          +item.formOfEmployment
        )
      })
      this.list = res.data.rows
      this.page.total = res.data.total
      console.log('员工列表', res)
    },
    handleSizeChange (size) {
      this.page.size = size
      this.page.page = 1
      this.getData()
    },
    handleCurrentChange (page) {
      this.page.page = page
      this.getData()
    },
    formatter (row, column, cellValue, index) {
      const result = employees.hireType.find(item => {
        return item.id === cellValue
      })
      return result ? result.value : '-'
    },
    // 新增按钮点击
    add () {
      this.show = true
    },
    // 删除
    del (id) {
      this.$confirm('您确定删除该员工吗？', '提示').then(async () => {
        await sysUserDel(id)
        this.$message.success('删除成功')
        this.handleCurrentChange(1)
      })
    },
    // 跳转到导入页面
    goImport () {
      this.$router.push(
        '/employees/import?name=' +
          encodeURIComponent('员工') +
          `&redirect=${encodeURIComponent('/employees')}`
      )
    },
    goDetail (id) {
      this.$router.push('/employees/details/' + id)
    },
    // 显示弹框
    imgClick (imgurl) {
      if (!imgurl) {
        this.$message.error('暂无图片')
        return
      }
      this.showQrcode = true
      this.$nextTick(() => {
        // console.log(this.$refs.canvas, imgurl)
        QrCode.toCanvas(this.$refs.canvas, imgurl, {
          width: 300,
          height: 300
        })
      })
    },
    // 角色点击
    roleClick (id) {
      this.$refs.dialogRole.isShow = true
      this.$refs.dialogRole.getUserInfo(id)
    }
  }
}
</script>
<style lang="scss" scoped>
.employees {
  padding: 15px;
  .btn {
    text-align: right;
    margin-bottom: 15px;
  }
  .page {
    padding-top: 30px;
    text-align: right;
  }
  .avatar {
    width: 30px;
    height: 30px;
    object-fit: contain;
  }
}
</style>
